<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改首页轮播')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-banner-edit" th:object="${lmBanner}">
            <input name="id" th:field="*{id}" type="hidden">
            <input name="bannerPic" th:field="*{bannerPic}" type="hidden">
            <div class="form-group">
                <label class="font-noraml">图片</label>
                <div class="file-loading">
                    <input id="fileinput-demo-1" type="file" multiple data-min-file-count="1" data-max-file-count="1" data-theme="fas">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="bannerTitle" th:field="*{bannerTitle}" class="form-control" type="text">
                </div>
            </div>
            <!--<div class="form-group">
               <label class="col-sm-3 control-label">富文本：</label>
               <div class="col-sm-8">
                   <input name="bannerUrl" th:field="*{bannerUrl}" class="form-control" type="text">
               </div>
           </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">banner富文本：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{bannerUrl}">
                    <div class="summernote" id="bannerUrl"></div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "lm/banner";

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });


        $(document).ready(function () {
            debugger;
            $("#fileinput-demo-1").fileinput({
                'uploadUrl': '#',
                overwriteInitial: true,
                initialPreviewAsData: true,
                showUpload:false,
                initialPreview: [
                   $("#bannerPic").val()
                ]
            });
        });
        $("#form-banner-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                uploadFile();
            }
        }

        function uploadFile() {
            debugger;
            var formData = new FormData();
            if($('#fileinput-demo-1')[0].files.length>0){
                formData.append('fileName', $("#fileinput-demo-1").val());
                formData.append('file', $('#fileinput-demo-1')[0].files[0]);
            }
            formData.append('bannerTitle',$("#bannerTitle").val());
            formData.append('bannerUrl',$("#bannerUrl").val());
            formData.append('id',$("#id").val());
            $.ajax({
                url: prefix + "/edit",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    </script>
</body>
</html>